<template>
  <a-modal :title="title" :width="800" :visible="visible" :confirm-loading="confirmLoading" cancel-text="关闭" @ok="submit" @cancel="closeModal">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="品牌名字">
          <a-input v-decorator="['name', validatorRules.val ]" placeholder="请输入品牌名字" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="是否启用">
          <a-radio-group v-decorator="['status', {initialValue: 1}, validatorRules.val ]" name="status">
            <a-radio :value="0">关闭</a-radio>
            <a-radio :value="1">启用</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import ApiTree from '@/api/goods/goodsBrand';
import { Slime } from '@/mixins/slime';
import { evaFiltersMixin } from '@/mixins/evaMixin';
export default {
  name: 'reportListModal',
  mixins: [Slime, evaFiltersMixin],
  data() {
    return {
      title: '编辑',
      form: this.$form.createForm(this),
      model: {},
      hospitalUl: [],
      validatorRules: {
        val: { rules: [{ required: true, message: '请输入内容!' }] },
      },
      id: 0,
    };
  },
  created() {},
  methods: {
    // 增
    crudCreate() {
      this.crudUpdate({});
    },
    // 改
    crudUpdate(record) {
      this.form.resetFields(); //重新设置领域
      this.visible = true;
      if (!record.id) {
        return false;
      }
      ApiTree.getDetail(record.id)
        .then((res) => {
          let data = res.result;
          this.id = data.id;
          this.form.setFieldsValue({
            name: data.name,
            status: data.status
          });
        })
        .finally(() => {});
    },
    // 提交
    submit() {
      const that = this;
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let param = values;
          if (!that.id) {
            that.submitCreate(param);
          } else {
            param = { ...param, id: that.id };
            that.submitUpdate(param);
          }
        }
      });
    },
    // 提交 增
    submitCreate(param) {
      const that = this;
      ApiTree.getAdd(param)
        .then((res) => {
          that.$message.success('新增成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    // 提交 改
    submitUpdate(param) {
      const that = this;
      ApiTree.getUpdate(param)
        .then((res) => {
          that.$message.success('编辑成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    // 弹窗-关闭
    closeModal() {
      this.confirmLoading = false;
      this.visible = false;
      this.id = null;
      this.form.resetFields();
      this.$emit('closeModal');
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>